package com.xiaoyu.client.ui.activity.nearby;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.loader.banner.BannerImageLoader;
import com.xiaoyu.client.model.nearby.ShopDetailBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.UIUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商铺详情页
 * Created by jk 2018/11/14
 */
public class ShopDetailActivity extends BaseActivityWithTitle {
    @BindView(R.id.activity_shop_details_banner)
    Banner mBanner;//轮播图
    private String storeId;//商铺id
    @BindView(R.id.activity_shop_detail_store_name)
    TextView mStoreName;//商铺名称
    @BindView(R.id.activity_shop_detail_address_txt)
    TextView mAddressTxt;//详细地址
    @BindView(R.id.activity_shop_detail_phone_txt)
    TextView mMobileTxt;//店主电话
    @BindView(R.id.activity_shop_details_mobile)
    TextView mPhoneTxt;//备用电话
    @BindView(R.id.activity_shop_detail_store_tag_layout)
    LinearLayout tagLayout;//商铺标签类型容器
    @BindView(R.id.activity_shop_detail_phone_layout)
    RelativeLayout phoneLayout;//店主电话item
    private List<String> bannerImgs = new ArrayList<>();//轮播图数据列表
    private ShopDetailBean bean;

    @Override
    protected void loadContentView(ViewGroup parent) {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_NEARBY_STORE) {
                storeId = bundle.getString(ClientConstants.STORE_ID);
            }
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_NEARBY_STORE_SEARCH) {
                storeId = bundle.getString(ClientConstants.STORE_ID);
            }
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_NEARBY_STORE_TAG) {
                storeId = bundle.getString(ClientConstants.STORE_ID);
            }
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_NEARBY_STORE_MY_RECOM) {
                storeId = bundle.getString(ClientConstants.STORE_ID);
            }
        }

        setTitleText("商家详情");
        LayoutInflater.from(this).inflate(R.layout.activity_shop_detail, parent, true);
        ButterKnife.bind(this);
        initBanner();
    }

    //初始化轮播图
    private void initBanner() {
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置是否自动轮播（不设置则默认自动）
        mBanner.isAutoPlay(true);
        //设置是否允许手动滑动轮播图
        mBanner.setViewPagerIsScroll(true);
        //设置轮播图片间隔时间（不设置默认为2000）
        mBanner.setDelayTime(2000);
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        bannerImgs.clear();
        getShopInfo();
    }


    /**
     * 获取商铺信息
     */
    private void getShopInfo() {
        showLoading();
        NetworkManager.getShopDetailInfo(storeId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                dissmissLoading();
                parseShopInfoSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                dissmissLoading();
            }
        });
    }

    private void parseShopInfoSuc(String result) {
        bean = new Gson().fromJson(result, ShopDetailBean.class);
        mStoreName.setText(bean.getData().getStoreName());
        mAddressTxt.setText(bean.getData().getStoreCity() + bean.getData().getStoreAddress());
        mMobileTxt.setText(bean.getData().getStore_owner_tel());
        mPhoneTxt.setText(bean.getData().getStore_owner_tel2());

        tagLayout.removeAllViews();
        for (int i = 0; i < bean.getData().getStoreTags().size(); i++) {
            TextView textView = new TextView(this);
            textView.setPadding(UIUtils.dip2px(10), 0, UIUtils.dip2px(10), 0);
            textView.setBackgroundColor(Color.parseColor(bean.getData().getStoreTags().get(i).getTagColor()));
            textView.setText(bean.getData().getStoreTags().get(i).getTagName());
            textView.setTextSize(11);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.WHITE);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, UIUtils.dip2px(22));
            if (i == 0) {
                layoutParams.setMargins(0, 0, 0, 0);
            } else {
                layoutParams.setMargins(UIUtils.dip2px(12), 0, 0, 0);
            }
            tagLayout.addView(textView, layoutParams);
        }

        bannerImgs.clear();
        bannerImgs.addAll(bean.getData().getStoreImage());
        mBanner.setImageLoader(new BannerImageLoader());
        mBanner.setImages(bannerImgs);
        mBanner.start();
    }


    /**
     * 店主电话item
     */
    @OnClick(R.id.activity_shop_detail_phone_layout)
    public void telItemClick() {
        diallPhone(mMobileTxt.getText().toString());
    }

    /**
     * 备用电话按钮
     */
    @OnClick(R.id.activity_shop_detail_phone_call_btn)
    public void phoneBtnClick() {
        diallPhone(mPhoneTxt.getText().toString());
    }

    /**
     * 调起高德或百度
     */
    @OnClick(R.id.activity_shop_detail_address_layout)
    public void mapRelativeClick() {
        new TransferMap(this, bean.getData().getStoreCity() + bean.getData().getStoreAddress());
    }


    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void diallPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }


    //如果你需要考虑更好的体验，可以这么操作
    @Override
    protected void onStart() {
        super.onStart();
        //开始轮播
        mBanner.startAutoPlay();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //结束轮播
        mBanner.stopAutoPlay();
    }
}
























